#!/usr/bin/perl
#
# Match a set of registered Automach machines to their entries
# in the regular database
#
use Counter;
use Carp;
use DBI;
use Getopt::Long;
use NoteHandler;

$users = Counter::open();
$machines = Counter::openfile("machines");
$DEBUG = 0;

$dbh = $FileTable::dbhandle;
$count = 100;
$recheck = 0;
$sparse = 0;
$days = 0;

GetOptions("count=i" => \$count,
	   "debug" => \$DEBUG,
	   "recheck" => \$recheck,
	   "sparse" => \$sparse,
	   "days=i" => \$days,
	   );

if ($days) {
    warn "Selecting last $days days of updates\n";
    $daylim = "and updated > '" . Counter::timestring(time() - $days*86400) . "'";
} else {
    $daylim = "";
}

if ($recheck) {
    $sth = $dbh->prepare("SELECT uniqueid, name, f_key, f_owner, rectext, mailfrom
	FROM automach
        WHERE rectext LIKE '%key:%' $daylim
        ORDER BY updated DESC  LIMIT $count");
} else {
    $sth = $dbh->prepare("SELECT uniqueid, name, f_key, f_owner, rectext, mailfrom
	FROM automach
        WHERE rectext LIKE '%key:%' and f_key = 0 $daylim
        ORDER BY updated DESC  LIMIT $count");
}

$sth->execute() || die "Unable to pick machine data\n";
$n = new NoteHandler;
while ($rec = $sth->fetchrow_hashref()) {
    my $owner = 0;
    my $key = 0;
    my $email = $$rec{mailfrom};
    $n->start($$rec{uniqueid});
    $n->head("$$rec{name} from $$rec{mailfrom}");
    # warn "$$rec{uniqueid} $$rec{name} starting\n";
    # note - some have owner nbr-pw; this ignores that
    if ($$rec{rectext} =~ /\bowner:\s*(\d+)/) {
	$owner = $1;
    }
    if ($$rec{rectext} =~ /\bkey:\s*(\d+)/m) {
	$key = $1;
    }
    if ($key) {
	$mach = $machines->get($key);
	if ($mach) {
	    if ($owner) {
		if ($owner != $mach->{owner}) {
		    $n->note("Owner inconsistent: $owner, $$mach{owner}");
		    $n->done("Owner inconsistent");
		} else {
		    if ($$rec{f_key} eq $key && $$rec{f_owner} eq $owner) {
			# do not do anything - it is OK
			$DEBUG && $n->note("matched $$rec{name} to $$mach{name} - already noted");
			$n->done("OK");
		    } else {
			$n->note("matched $$rec{name} to $$mach{name}");
			$n->note("Updating automach record");
			$stm = $dbh->prepare("UPDATE automach
                             SET f_key = ?, f_owner = ?
                             WHERE uniqueid = ?");
			$stm->execute($key, $owner, $$rec{uniqueid})    || die "Unable to update f_key and f_owner\n";
			$n->done("Updated");
		    }
		}
	    } else {
		$n->note("found mach, owner unspecified");
		$user = $users->get($$mach{owner});
		$n->note("Machine owner is $$mach{owner} - $$user{email}");
		if ($$rec{mailfrom} =~ /$$user{email}/) {
		    $n->note("Emails match");
		    $n->done("Owner unspecified, machine exists, emails match");
		} else {
		    $n->done("Owner unspecified, machine exists");
		}
	    }
	} else {
	    if ($owner) {
		$user = $users->get($owner);
		if ($user) {
		    # could give him a machine record here....
		    $n->note("owner $owner exists, but not mach $key")
			if !$sparse;
		    $n->done("Owner exists, but not machine");
		} else {
		    # special for checking against reduced DB
		    $n->note("mach $key and user $owner not found")
			if !$sparse;
		    $n->done("Machine and user not found");
		}
	    } else {
		$n->note("key $key not found") if !$sparse;
		$n->done("Machine not found");
	    }
	}
    } else {
	$n->note("no key in record") if $DEBUG;
	$n->done("No key in record");
    }
}

$n->report();


sub note {
    my $rec = shift;
    my $text = shift;

    croak "Old note sub called";
    if ($$rec{uniqueid} ne $prevuniqueid) {
	$prevuniqueid = $$rec{uniqueid};
    }
    warn "$$rec{uniqueid}: $text\n";
}

    


	    
